﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntryEngine.Network;
using EntryEngine;
using System.Xml.Linq;
using EntryEngineServer;

namespace Server
{
    public class Plugin : IPlugin
    {
        ConnectionPool pool;
        void IDisposable.Dispose()
        {
            AsyncThread.QueueUserWorkItem(() =>
            {
                Thread.Sleep(pool.ClearTime);
                pool.ClearPool();
                if (pool.WorkingCount == 0)
                {
                    pool.Dispose();
                    _LOG.Info("卸载旧连接池");
                    return true;
                }
                return false;
            });
        }

        object IPlugin.Inject(params object[] param)
        {
            string dbconn = (string)param[0];
            string dbname = (string)param[1];
            bool isRebuild = (bool)param[2];
            _LOG.Info("设置数据库：{0} 连接字符串：{1}", dbname, dbconn);

            var _DB = new _DB();
            // 设置数据库
            _DB.IsDropColumn = true;
            _DB.DatabaseName = dbname;
            _DB._DAO = pool = new ConnectionPool(new MYSQL_DATABASE());
            if (isRebuild)
            {
                _DB.IsDropColumn = true;
                _DB._DAO.ConnectionString = dbconn;
            }
            else
            {
                _DB._DAO.ConnectionString = string.Format("{0}Database={1};", dbconn, dbname);
                _DB._DAO.OnTestConnection -= _DB.UPDATE_DATABASE_STRUCTURE;
            }
            _DB._DAO.TestConnection();
            return _DB;
        }
    }
}
